﻿// <copyright file="CleanableKingsQueueFiller.cs" company="Ecole Normale Supérieure">
// Released under GPLv2
// </copyright>
// <summary>Contains a daemon to fill in the queue of kings to clean logs, attacks, etc.</summary>

namespace Sqwarea.Batch
{
    using System;
    using Lokad.Cloud.ServiceFabric;
    using Lokad.Cloud.Services;
    using Lokad.Cloud.Storage;
    using Sqwarea.GameLogic.Connexity;
    using Sqwarea.Persistence;

    /// <summary>
    /// A daemon to fill in the queue of kings (in their id) to clean logs, attacks, etc.
    /// </summary>
    [ScheduledServiceSettings(
        AutoStart = true,
        ProcessingTimeoutSeconds = GlobalParameters.CleanableKingsQueueFillerTimeOutSeconds,
        TriggerInterval = GlobalParameters.CleanableKingsQueueFillerIntervalSecond)]
    public class CleanableKingsQueueFiller : ScheduledService
    {
        /// <summary>
        /// Gets or sets game provider (given by AutoFac Property Injection)
        /// </summary>
        public IGameProvider GameProvider { get; set; }

        /// <summary>
        /// Gets or sets queue provider (given by AutoFac Property Injection)
        /// </summary>
        public IQueueProvider QueueProvider { get; set; }

        /// <summary>
        /// Scheduled run of a connexity issue checking
        /// </summary>
        protected override void StartOnSchedule()
        {
            if (this.QueueProvider.GetCleanableKingIdQueueLength() <= GlobalParameters.CleanableKingIdQueueThreshold)
            {
                var allkings = this.GameProvider.GetAllKings();
                foreach (var king in allkings)
                {
                    this.QueueProvider.PutKingToClean(king.Id);
                }
            }
        }
    }
}
